﻿
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#include "CPtrList.h"
bool GetProfileString(char key[],char value[],char str[],int strlen,char filename[])
{
    int i;
    
    FILE* file = fopen(filename,"r");
    if(file == 0) return false;
    
    char buffer[1024];
    while(!feof(file))
    {
        char * p = fgets(buffer,1024,file);
        if(p == 0) break;
        
        if(buffer[0] == '[')
        {
            for(i=0;buffer[i] != 0;i++)
                if(buffer[i] == ']') break;
                
            if(buffer[i] != ']') continue;
            
            buffer[i] = 0;
            
            if(strcmp(key,&buffer[1]) != 0) continue;
            
            while(!feof(file))
            {
                char* p = fgets(buffer,1024,file);
                if(p == 0) break;
                if(buffer[0] == '[') break;
                
                for(i=0;i<buffer[i]!=0;i++)
                    if(buffer[i] == '=') break;
                    
                if(buffer[i] != '=') break;
                buffer[i] = 0;
                if(strcmp(buffer,value) != 0) continue;
                
                strncpy(str,&buffer[i+1],strlen);

       for(int k=0;k<strlen;k++)
       {
         if(str[k] == 0) break;
         if(str[k] == 0x0a || str[k] == 0x0d)
         {
           str[k] = 0;
           break;
         }
       }
        
                fclose(file);
                return true;
            }
        }
    }

    fclose(file);
    
    return false;
}

bool PutProfileString(char key[],char value[],char str[],char filename[])
{
    int i;
                 
    FILE* file = fopen(filename,"r");
    if(file == 0)
    {
        file = fopen(filename,"w");
        if(file == 0) return false;
        char buffer[256];
        sprintf(buffer,"[%s]\n",key);
        fwrite(buffer,1,strlen(buffer),file);
        sprintf(buffer,"%s=%s\n",value,str);
        fwrite(buffer,1,strlen(buffer),file);
        fclose(file);
        return true;
    }
    
    char buffer[1024];
    bool bHasSection = false;
    while(!feof(file))
    {
        char * p = fgets(buffer,1024,file);
        if(p == 0) break;
        
        if(buffer[0] == '[')
        {
            for(i=0;buffer[i] != 0;i++)
                if(buffer[i] == ']') break;
                
            if(buffer[i] != ']') continue;
            
            buffer[i] = 0;
            
            if(strcmp(key,&buffer[1]) != 0) continue;
        
            bHasSection = true;
            break;
        }
    }	
    
    if(!bHasSection)
    {
        fclose(file);
        file = fopen(filename,"r+");
        if(file == 0) return false;
        fseek(file,0,SEEK_END);
        
        char buffer[256];
        sprintf(buffer,"[%s]\n",key);
        fwrite(buffer,1,strlen(buffer),file);
        sprintf(buffer,"%s=%s\n",value,str);
        fwrite(buffer,1,strlen(buffer),file);
        fclose(file);
        return true;
    }
    else
    {
        bool bHasKey = false;
        long pos1,pos2;
        
        pos1 = ftell(file);
        while(!feof(file))
        {
            pos1 = ftell(file);
            char* p = fgets(buffer,1024,file);
            pos2 = ftell(file);
            if(p == 0) break;
            if(buffer[0] == '[') break;
                
            for(i=0;i<buffer[i]!=0;i++)
                if(buffer[i] == '=') break;
                    
            if(buffer[i] != '=') break;
            buffer[i] = 0;
        
            if(strcmp(buffer,value) != 0) continue;
            
            bHasKey = true;
            break;	
        }
        
        fseek(file, 0L, SEEK_END );
            long len = ftell( file );
        
        char* pbuf = new char[len];
        fseek(file,0L,SEEK_SET);
        fread(pbuf,1,len,file);
        fclose(file);
                        
        file = fopen(filename,"w");
                    
        if(bHasKey)
        {
            fwrite(pbuf,1,pos1,file);
            char buffer[256];
            sprintf(buffer,"%s=%s\n",value,str);
            fwrite(buffer,1,strlen(buffer),file);
            fwrite(pbuf+pos2,1,len-pos2,file);
        }
        else
        {
            fwrite(pbuf,1,pos1,file);
            char buffer[256];
            sprintf(buffer,"%s=%s\n",value,str);
            fwrite(buffer,1,strlen(buffer),file);
            fwrite(pbuf+pos1,1,len-pos1,file);
        }
        
        delete pbuf;
    }

    fclose(file);
    

    return true;
}

CATMPtrList::CATMPtrList()
{
    m_nCount = 0;
    m_mutex = CreateMutex(NULL, false, NULL);
    
}

CATMPtrList::~CATMPtrList()
{
    
}
    
bool CATMPtrList::AddTail(void* ptr)
{
    bool r = false;
    
  WaitForSingleObject(m_mutex, INFINITE);
    
    if(m_nCount < 1024)
    {
        m_ptr[m_nCount] = ptr;
        m_nCount++;
        r = true;
    }

    ReleaseMutex(m_mutex);
    
    return r;
}
    
void* CATMPtrList::RemoveHead()
{
    WaitForSingleObject(m_mutex, INFINITE);

    void* p = NULL;
    if(m_nCount > 0)
    {
        p = m_ptr[0];
        for(int i=1;i<m_nCount;i++)
        {
            m_ptr[i-1] = m_ptr[i];
        }
        m_nCount --;
    }
    ReleaseMutex(m_mutex);
    
    return p;
}
    
int CATMPtrList::GetCount()
{
    return m_nCount;
}
    
void* CATMPtrList::GetAt(int index)
{
  if(index > m_nCount-1) return NULL;
  void* p = NULL;
  WaitForSingleObject(m_mutex, INFINITE);
  p = m_ptr[index];
    ReleaseMutex(m_mutex);
  return p;
}

void* CATMPtrList::RemoveAt(int index)
{
  if(index > m_nCount-1) return NULL;
  void* p = NULL;
  WaitForSingleObject(m_mutex, INFINITE);
  p = m_ptr[index];
  for(int i=index;i<m_nCount-1;i++)
    m_ptr[i] = m_ptr[i+1];
  m_nCount--;
    ReleaseMutex(m_mutex);
  return p;
}

bool CATMPtrList::InsertAt(void* ptr,int index)
{
    if(m_nCount >= 1024) return false;
  if(index > m_nCount) return false;
  
  WaitForSingleObject(m_mutex, INFINITE);
  for(int i=m_nCount-1;i>=index;i--)
  {
    m_ptr[i+1] = m_ptr[i];
  }
  m_ptr[index] = ptr;
  m_nCount++;
  ReleaseMutex(m_mutex);

  return true;
}
